package com.tjetc.adminmanage;


import com.tjetc.domian.Resident;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ResidentService;
import com.tjetc.view.AdminMainView;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

//居民信息管理
public class ResidentManageView {
    private static Scanner scanner = new Scanner(System.in);
    private static ResidentService residentService = ClientProxy.getProxyInstance(ResidentService.class);

    public void start() {
        while (true) {
            System.out.println("居民信息管理菜单");
            System.out.println("1.查询居民信息");
            System.out.println("2.修改居民信息");
            System.out.println("3.删除居民信息");
            System.out.println("4.添加居民信息");
            System.out.println("5.查询该居民所在的公寓id");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    updata();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    add();
                    break;
                case 5:
                    getApartmentId();
                    break;
                case 0:
                    new AdminMainView().start();
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }

    }


    private void getApartmentId() {
        System.out.println("查询该居民所在的公寓id");
        System.out.println("输入居民姓名");
        String name = scanner.next();
        Result result = residentService.getApartmentIdByname(name);
        System.out.println(result.getData());
    }

    private void add() {
        System.out.println("添加居民信息");
        System.out.println("请输入居民姓名");
        String name = scanner.next();
        System.out.println("请输入性别");
        String gender = scanner.next();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        System.out.println("请输入公寓id");
        int apartmentid = scanner.nextInt();
        System.out.println("请输入电话");
        String contact = scanner.next();
        System.out.println("请输入邮箱");
        String email = scanner.next();
        Resident resident = new Resident(name, gender, age, apartmentid, contact, email);
        Result result = residentService.add(resident);
        System.out.println(result.getMsg());
    }

    private void del() {
        System.out.println("删除居民信息");
        System.out.println("输入居民id");
        int id = scanner.nextInt();
        Result result = residentService.del(id);
        System.out.println(result.getMsg());
    }

    private void updata() {
        System.out.println("修改居民信息");
        System.out.println("输入居民id");
        int id = scanner.nextInt();
        System.out.println("输入更改后的公寓号");
        int apartmentid = scanner.nextInt();
        System.out.println("输入更改后的手机号");
        String contact = scanner.next();
        System.out.println("输入更改后的邮箱");
        String email = scanner.next();
        Resident resident = new Resident();
        resident.setApartmentid(apartmentid);
        resident.setContact(contact);
        resident.setEmail(email);
        Result result = residentService.update(resident);
        System.out.println(result.getMsg());

    }

    private void list() {
        List<Resident> list = residentService.select();
        for (Resident resident : list) {
            System.out.println(resident);
        }
    }
}
